package ru.spb.osll.myfit.server.handler;

import java.io.File;
import java.util.List;

import org.easymock.EasyMock;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import ru.spb.osll.myfit.common.Content;
import ru.spb.osll.myfit.common.TreeNode;
import ru.spb.osll.myfit.util.FileHelper;

public class ContentCreatorTest {
	/*
	  Test case:
	        root-+
	             |
	             +-content.txt
		     |
                     +-TestPage1-+
                     |           |
                     |           +-content.txt
	             |
	             +-TestPage2-+
	             |           |
	             |           +-content.txt
	             |           |
	             |           +-TestPage3-+
	             |                       |
	             |                       +-content.txt
	             |
	             +-EmptyDir

	  Expected content tree:
		*-+
		  |
		  +-TestPage1
		  |
		  +-TestPage2-+
		              |
		              +-TestPage3

		prefix: "root"

	 */

	private String root = "FitNesseRoot";
	private String topDir = root + File.separator + "root" + File.separator;
	private String testPage1 = topDir + File.separator + "TestPage1" + File.separator;
	private String testPage2 = topDir + File.separator + "TestPage2" + File.separator;
	private String testPage3 = testPage2 + File.separator + "TestPage3" + File.separator;
	private String emptyDir = topDir + File.separator + "EmptyDir" + File.separator;

	private File topDirFile = new File(topDir);

	private File contentTopDir = new File(topDir + "content.txt");
	private File contentTestPage1 = new File(testPage1 + "content.txt");
	private File contentTestPage2 = new File(testPage2 + "content.txt");
	private File contentTestPage3 = new File(testPage3 + "content.txt");

	private File testPage1File = new File(testPage1);
	private File testPage2File = new File(testPage2);
	private File testPage3File = new File(testPage3);
	private File emptyDirFile = new File(emptyDir);

	private File[] topDirFiles = {contentTopDir, testPage1File, testPage2File, emptyDirFile};

	private File[] testPage1Files = {contentTestPage1};
	private File[] testPage2Files = {contentTestPage2, testPage3File};
	private File[] testPage3Files = {contentTestPage3};

	private String[] testPage1Strings = {"content.txt"};
	private String[] testPage2Strings = {"content.txt", "TestPage3"};
	private String[] testPage3Strings = {"content.txt"};
	private String[] emptyDirStrings = {};

	@Test
	public void testCreateContent() {
		FileHelper helper = EasyMock.createStrictMock(FileHelper.class);

		EasyMock.expect(helper.getFile(topDir + "content.txt")).andReturn(contentTopDir);
		EasyMock.expect(helper.listFiles(topDirFile)).andReturn(topDirFiles);
		EasyMock.expect(helper.isDirectory(contentTopDir)).andReturn(false);
		EasyMock.expect(helper.isDirectory(testPage1File)).andReturn(true);

			EasyMock.expect(helper.isDirectory(testPage1File)).andReturn(true);
			EasyMock.expect(helper.list(testPage1File)).andReturn(testPage1Strings);
			EasyMock.expect(helper.listFiles(testPage1File)).andReturn(testPage1Files);
			EasyMock.expect(helper.isDirectory(contentTestPage1)).andReturn(false);

		EasyMock.expect(helper.isDirectory(testPage2File)).andReturn(true);

			EasyMock.expect(helper.isDirectory(testPage2File)).andReturn(true);
			EasyMock.expect(helper.list(testPage2File)).andReturn(testPage2Strings);
			EasyMock.expect(helper.listFiles(testPage2File)).andReturn(testPage2Files);
			EasyMock.expect(helper.isDirectory(contentTestPage2)).andReturn(false);
			EasyMock.expect(helper.isDirectory(testPage3File)).andReturn(true);

				EasyMock.expect(helper.isDirectory(testPage3File)).andReturn(true);
				EasyMock.expect(helper.list(testPage3File)).andReturn(testPage3Strings);
				EasyMock.expect(helper.listFiles(testPage3File)).andReturn(testPage3Files);
				EasyMock.expect(helper.isDirectory(contentTestPage3)).andReturn(false);

		EasyMock.expect(helper.isDirectory(emptyDirFile)).andReturn(true);

			EasyMock.expect(helper.isDirectory(emptyDirFile)).andReturn(true);
			EasyMock.expect(helper.list(emptyDirFile)).andReturn(emptyDirStrings);

		EasyMock.replay(helper);

		ContentCreator cc = new ContentCreator();
		cc.setHelper(helper);
		Content c = cc.createContent(root, "root", new Integer(3));

		assertTrue(c != null);
		assertEquals("root", c.getPath());

		TreeNode root = c.getTreeNode();
		List<TreeNode> rootChildren = root.getChildren();
		assertTrue(rootChildren.size() == 2);

		TreeNode testPage1root = rootChildren.get(0);
		assertTrue(testPage1root.getChildren() == null);

		TreeNode testPage2root = rootChildren.get(1);
		List<TreeNode> testPage2Children = testPage2root.getChildren();
		assertTrue(testPage2Children.size() == 1);

		TreeNode testPage3root = testPage2Children.get(0);
		assertTrue(testPage3root.getChildren() == null);

		EasyMock.verify(helper);
	}
}
